<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属用户ID" prop="sysUserId">
        <el-input v-model="queryParams.sysUserId" placeholder="请输入所属用户ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="账户ID" prop="accountId">
        <el-input v-model="queryParams.accountId" placeholder="请输入账户ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="店铺ID" prop="shopId">
        <el-input v-model="queryParams.shopId" placeholder="请输入店铺ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="货号" prop="skuId">
        <el-input v-model="queryParams.skuId" placeholder="请输入货号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否在售" prop="addOnDeal">
        <el-select v-model="queryParams.addOnDeal" placeholder="请选择是否在售" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="冷却秒" prop="boostCoolDownSeconds">
        <el-input v-model="queryParams.boostCoolDownSeconds" placeholder="请输入冷却秒" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否脱销" prop="deboosted">
        <el-select v-model="queryParams.deboosted" placeholder="请选择是否脱销" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="标识" prop="flag">
        <el-input v-model="queryParams.flag" placeholder="请输入标识" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否活动" prop="inPromotion">
        <el-select v-model="queryParams.inPromotion" placeholder="请选择是否活动" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="喜欢数量" prop="likeCount">
        <el-input v-model="queryParams.likeCount" placeholder="请输入喜欢数量" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="父SKU" prop="parentSku">
        <el-input v-model="queryParams.parentSku" placeholder="请输入父SKU" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="销售数量" prop="sold">
        <el-input v-model="queryParams.sold" placeholder="请输入销售数量" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="已有库存" prop="sellerCurrentStock">
        <el-input v-model="queryParams.sellerCurrentStock" placeholder="请输入已有库存" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="浏览量" prop="viewCount">
        <el-input v-model="queryParams.viewCount" placeholder="请输入浏览量" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否在售" prop="wholesale">
        <el-select v-model="queryParams.wholesale" placeholder="请选择是否在售" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类状态" prop="categoryStatus">
        <el-select v-model="queryParams.categoryStatus" placeholder="请选择分类状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="邮寄天数" prop="daysToShip">
        <el-input v-model="queryParams.daysToShip" placeholder="请输入邮寄天数" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否预售" prop="preOrder">
        <el-select v-model="queryParams.preOrder" placeholder="请选择是否预售" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="品牌ID" prop="brandId">
        <el-input v-model="queryParams.brandId" placeholder="请输入品牌ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="品牌名称" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入品牌名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="虾皮修改时间" prop="modifyTime">
        <el-date-picker v-model="queryParams.modifyTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="是否B2C店铺" prop="isB2CSkuInFbsShop">
        <el-select v-model="queryParams.isB2CSkuInFbsShop" placeholder="请选择是否B2C店铺" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="MT商品ID" prop="mtskuItemId">
        <el-input v-model="queryParams.mtskuItemId" placeholder="请输入MT商品ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="一级分类" prop="category1">
        <el-input v-model="queryParams.category1" placeholder="请输入一级分类" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="二级分类" prop="category2">
        <el-input v-model="queryParams.category2" placeholder="请输入二级分类" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="三级分类" prop="category3">
        <el-input v-model="queryParams.category3" placeholder="请输入三级分类" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="参与活动列表" prop="ongoingUpcomingCampaigns">
        <el-input v-model="queryParams.ongoingUpcomingCampaigns" placeholder="请输入参与活动列表" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['shopee:product:create']">新增</el-button> -->
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleRefresh"
                   v-hasPermi="['shopee:product:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['shopee:product:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="所属用户ID" align="center" prop="sysUserId" />
      <el-table-column label="账户ID" align="center" prop="accountId" />
      <el-table-column label="店铺ID" align="center" prop="shopId" />
      <el-table-column label="货号" align="center" prop="skuId" />
      <el-table-column label="是否在售" align="center" prop="addOnDeal" />
      <el-table-column label="冷却秒" align="center" prop="boostCoolDownSeconds" />
      <el-table-column label="是否脱销" align="center" prop="deboosted" />
      <el-table-column label="标识" align="center" prop="flag" />
      <el-table-column label="是否活动" align="center" prop="inPromotion" />
      <el-table-column label="喜欢数量" align="center" prop="likeCount" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="父SKU" align="center" prop="parentSku" />
      <el-table-column label="销售数量" align="center" prop="sold" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="已有库存" align="center" prop="sellerCurrentStock" />
      <el-table-column label="浏览量" align="center" prop="viewCount" />
      <el-table-column label="是否在售" align="center" prop="wholesale" />
      <el-table-column label="分类状态" align="center" prop="categoryStatus" />
      <el-table-column label="邮寄天数" align="center" prop="daysToShip" />
      <el-table-column label="是否预售" align="center" prop="preOrder" />
      <el-table-column label="品牌ID" align="center" prop="brandId" />
      <el-table-column label="品牌名称" align="center" prop="brand" />
      <el-table-column label="虾皮修改时间" align="center" prop="modifyTime" />
      <el-table-column label="是否B2C店铺" align="center" prop="isB2CSkuInFbsShop" />
      <el-table-column label="MT商品ID" align="center" prop="mtskuItemId" />
      <el-table-column label="一级分类" align="center" prop="category1" />
      <el-table-column label="二级分类" align="center" prop="category2" />
      <el-table-column label="三级分类" align="center" prop="category3" />
      <el-table-column label="参与活动列表" align="center" prop="ongoingUpcomingCampaigns" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <!-- <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['shopee:product:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['shopee:product:delete']">删除</el-button>
        </template> -->
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属用户ID" prop="sysUserId">
          <el-input v-model="form.sysUserId" placeholder="请输入所属用户ID" />
        </el-form-item>
        <el-form-item label="账户ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账户ID" />
        </el-form-item>
        <el-form-item label="店铺ID" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺ID" />
        </el-form-item>
        <el-form-item label="货号" prop="skuId">
          <el-input v-model="form.skuId" placeholder="请输入货号" />
        </el-form-item>
        <el-form-item label="是否在售" prop="addOnDeal">
          <el-radio-group v-model="form.addOnDeal">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="冷却秒" prop="boostCoolDownSeconds">
          <el-input v-model="form.boostCoolDownSeconds" placeholder="请输入冷却秒" />
        </el-form-item>
        <el-form-item label="是否脱销" prop="deboosted">
          <el-radio-group v-model="form.deboosted">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标识" prop="flag">
          <el-input v-model="form.flag" placeholder="请输入标识" />
        </el-form-item>
        <el-form-item label="是否活动" prop="inPromotion">
          <el-radio-group v-model="form.inPromotion">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="喜欢数量" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入喜欢数量" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="父SKU" prop="parentSku">
          <el-input v-model="form.parentSku" placeholder="请输入父SKU" />
        </el-form-item>
        <el-form-item label="销售数量" prop="sold">
          <el-input v-model="form.sold" placeholder="请输入销售数量" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="已有库存" prop="sellerCurrentStock">
          <el-input v-model="form.sellerCurrentStock" placeholder="请输入已有库存" />
        </el-form-item>
        <el-form-item label="浏览量" prop="viewCount">
          <el-input v-model="form.viewCount" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="是否在售" prop="wholesale">
          <el-radio-group v-model="form.wholesale">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类状态" prop="categoryStatus">
          <el-radio-group v-model="form.categoryStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮寄天数" prop="daysToShip">
          <el-input v-model="form.daysToShip" placeholder="请输入邮寄天数" />
        </el-form-item>
        <el-form-item label="是否预售" prop="preOrder">
          <el-radio-group v-model="form.preOrder">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="品牌ID" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌ID" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="虾皮修改时间" prop="modifyTime">
          <el-date-picker clearable v-model="form.modifyTime" type="date" value-format="timestamp" placeholder="选择虾皮修改时间" />
        </el-form-item>
        <el-form-item label="是否B2C店铺" prop="isB2CSkuInFbsShop">
          <el-radio-group v-model="form.isB2CSkuInFbsShop">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="MT商品ID" prop="mtskuItemId">
          <el-input v-model="form.mtskuItemId" placeholder="请输入MT商品ID" />
        </el-form-item>
        <el-form-item label="一级分类" prop="category1">
          <el-input v-model="form.category1" placeholder="请输入一级分类" />
        </el-form-item>
        <el-form-item label="二级分类" prop="category2">
          <el-input v-model="form.category2" placeholder="请输入二级分类" />
        </el-form-item>
        <el-form-item label="三级分类" prop="category3">
          <el-input v-model="form.category3" placeholder="请输入三级分类" />
        </el-form-item>
        <el-form-item label="参与活动列表" prop="ongoingUpcomingCampaigns">
          <el-input v-model="form.ongoingUpcomingCampaigns" placeholder="请输入参与活动列表" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createProduct, updateProduct, deleteProduct, getProduct, getProductPage, exportProductExcel, refreshAll } from "@/api/shopee/product";

export default {
  name: "Product",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 虾皮商品列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        sysUserId: null,
        accountId: null,
        shopId: null,
        skuId: null,
        addOnDeal: null,
        boostCoolDownSeconds: null,
        deboosted: null,
        flag: null,
        inPromotion: null,
        likeCount: null,
        name: null,
        parentSku: null,
        sold: null,
        status: null,
        sellerCurrentStock: null,
        viewCount: null,
        wholesale: null,
        categoryStatus: null,
        daysToShip: null,
        preOrder: null,
        brandId: null,
        brand: null,
        modifyTime: [],
        isB2CSkuInFbsShop: null,
        mtskuItemId: null,
        category1: null,
        category2: null,
        category3: null,
        ongoingUpcomingCampaigns: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getProductPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        sysUserId: undefined,
        accountId: undefined,
        shopId: undefined,
        skuId: undefined,
        addOnDeal: undefined,
        boostCoolDownSeconds: undefined,
        deboosted: undefined,
        flag: undefined,
        inPromotion: undefined,
        likeCount: undefined,
        name: undefined,
        parentSku: undefined,
        sold: undefined,
        status: undefined,
        sellerCurrentStock: undefined,
        viewCount: undefined,
        wholesale: undefined,
        categoryStatus: undefined,
        daysToShip: undefined,
        preOrder: undefined,
        brandId: undefined,
        brand: undefined,
        modifyTime: undefined,
        isB2CSkuInFbsShop: undefined,
        mtskuItemId: undefined,
        category1: undefined,
        category2: undefined,
        category3: undefined,
        ongoingUpcomingCampaigns: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加虾皮商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改虾皮商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateProduct(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createProduct(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除虾皮商品编号为"' + id + '"的数据项?').then(function() {
          return deleteProduct(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有虾皮商品数据项?').then(() => {
          this.exportLoading = true;
          return exportProductExcel(params);
        }).then(response => {
          this.$download.excel(response, '虾皮商品.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 刷新店铺信息操作 */
    handleRefresh() {
      this.reset();
      refreshAll().then(response => {
          console.log(response.data);
          this.getList();
          this.$modal.msgSuccess("刷新成功");
      });
    },
  }
};
</script>
